package me.chendan.age

import org.apache.spark.{SparkConf, SparkContext}

/**
  * Created by chendan on 16-4-6.
  */
class AvgAge {

}

object AvgAge extends App {
  // 在SparkConf上设置的属性具有最高的优先级
  // 其次是传递给spark-submit或者spark-shell的属性值
  // 最后是spark-defaults.conf文件中的属性值
  // SparkConf > CLI > spark-defaults.conf
  val conf = new SparkConf().setAppName("PeopleAvgAgeCal")
  val sc = new SparkContext(conf)
  val data = sc.textFile("people.txt")
  val count = data.count()
  val ageData = data.map(line => line.split(" ")(1))

  val sum = ageData.map(age => age.toInt).collect().reduce((a, b) => a + b)
  //val sum = ageData.map(age => Integer.parseInt(String.valueOf(age))).collect().reduce((a, b) => a + b)
  println("sum: " + sum)
  printf("total: %s people, and the average age is: %s \n", count, (0.0+sum)/count)
}
